<?php 
namespace app\admin\controller;
use think\Db;
/**
 * 权限组
 */
class AuthGroup extends Base{

	public function index(){
		$page = $this->request->param('page/d',1);
		$limit = $this->request->param('limit/d',20);
		$keyword = $this->request->param('keyword','','trim');
		$where=NULL;
		if( $keyword ){
			$where = $keyword ? " `groupname` LIKE '%".$keyword."%'" : '1=1';
		}
		$totalCount = db('auth_group')->where($where)-> count();
		$list = db('auth_group')->where($where)->page($page,$limit)->order(['id'=>'asc'])->select();
		$data['code'] = 0;
		$data['data'] = $list;
		$data['count'] = $totalCount;
		$data['msg'] = '请求成功';

		if( $this->request->isAjax() ){
			return $data;
		}else{
			return $this->fetch();
		}
	}
	public function add(){
		if($this->request-> isPost() || $this->request-> isAjax()){
			$add['groupname'] = $this->request->param('groupname','','trim');
			$add['status'] = $this->request->param('status','0','trim');
			$add['isDel'] = $this->request->param('isDel','0','trim');
			$add['auth'] = '';

			$res = db('auth_group')->insert($add);
			if( $res != 1 ){
				addAdminLog('添加权限组，操作失败');
				$data= ['err'=>1,'msg'=>'操作失败！'];
			   	return $data;
			}else{
				addAdminLog('添加权限组，操作成功');
				$data= ['err'=>0,'msg'=>'操作成功！'];
			   	return $data;
			}
		}else{
			return $this->fetch();
		}
	}
	// public function status(){
	// 	if($this->request-> isPost() || $this->request-> isAjax()){
	// 		$upd['id'] = $this->request->param('id','','trim');
	// 		$upd['status'] = $this->request->param('status','0','trim');
	// 		$res = db('auth_group')->update($upd);
	// 		if( $res !== false ){
	// 			$data= ['err'=>0,'msg'=>'操作成功！'];
	// 		   	return $data;
	// 		}else{
	// 			$data= ['err'=>1,'msg'=>'操作失败！'];
	// 		   	return $data;
	// 		}
	// 	}else{
	// 		$data= ['err'=>1,'msg'=>'操作失败！'];
	// 		return $data;
	// 	}
	// }

	public function edit(){
		$id = $this->request->param('id','','trim');
		if($this->request-> isPost() || $this->request-> isAjax()){
			$upd['id'] = $id;
			$upd['status'] = $this->request->param('status','0','trim');
			$upd['isDel'] = $this->request->param('isDel','0','trim');
			$upd['groupname'] = $this->request->param('groupname','0','trim');
			$res = db('auth_group')->update($upd);
			if( $res !== false ){
				addAdminLog('编辑权限组，操作成功');
				$data= ['err'=>0,'msg'=>'操作成功！'];
			   	return $data;
			}else{
				addAdminLog('编辑权限组，操作失败');
				$data= ['err'=>1,'msg'=>'操作失败！'];
			   	return $data;
			}
		}else{
			$info = db('auth_group')->where('id = '.$id)->find();
			$this->assign('info',$info);
			return $this->fetch();
		}
	}

	public function del(){
		$id = $this->request->param('id','','trim');
		if($this->request-> isPost() || $this->request-> isAjax()){
			$res = db('auth_group')->where('id = '.$id)->delete();
			if( $res !== false ){
				addAdminLog('删除权限组，操作成功');
				$data= ['err'=>0,'msg'=>'操作成功！'];
			   	return $data;
			}else{
				addAdminLog('删除权限组，操作失败');
				$data= ['err'=>1,'msg'=>'操作失败！'];
			   	return $data;
			}
		}else{
			$data= ['err'=>1,'msg'=>'操作失败！'];
			return $data;
		}
	}

	public function editAuth(){
		$id = $this->request->param('id','','trim');
		if($this->request-> isPost() || $this->request-> isAjax()){
			$role =  $this->request->param('role'); 
			$upd['id'] = $id;
			$upd['auth'] = implode(',',$role);
			$res = db('auth_group')->update($upd);
			if( $res !== false ){
				addAdminLog('编辑权限组的权限，操作成功');
				$data= ['err'=>0,'msg'=>'操作成功！'];
			   	return $data;
			}else{
				addAdminLog('编辑权限组的权限，操作成功');
				$data= ['err'=>1,'msg'=>'操作失败！'];
			   	return $data;
			}
		}else{
			$auth =  db('auth_class')->where('status = 0')->order('order_num decs')->select();
			foreach($auth as $key=>$val){
				$auth[$key]['child'] = db('auth')->where(['status'=>0,'auth_class_id'=>$val['id']])->order('order_num decs')->select();
			}
			$auth_group = db('auth_group')->where('id = '.$id)->find();
			if( $auth_group['auth'] ){
				$auth_group['auth'] = explode(',',$auth_group['auth']);
			}
			if(!$auth_group['auth']){
				$auth_group['auth'] =[];
			}
			$this->assign('auth',$auth);
			$this->assign('auth_group',$auth_group);
			$this->assign('id',$id);
			return $this->fetch('editAuth');
		}
	}
}